package cn.livingCloud.controller.content.validator;

import cn.livingCloud.dto.ArticleFormDto;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Created by DingJie  on 2016/1/18.
 */
@Component
public class ArticleFormDtoValidator implements Validator {
    @Override
    public boolean supports(Class<?> aClass) {
        return ArticleFormDto.class.equals(aClass);
    }

    @Override
    public void validate(Object target, Errors errors) {
        ArticleFormDto formDto = (ArticleFormDto) target;
        validateTitleAndContent(formDto, errors);
    }

    private void validateTitleAndContent(ArticleFormDto formDto, Errors errors) {
        if (StringUtils.isEmpty(formDto.getTitle())) {
            errors.rejectValue("title", null, "文章标题不能为空");
        }
        if (StringUtils.isEmpty(formDto.getContent())) {
            errors.rejectValue("content", null, "文章内容不能为空");
        }
    }
}
